﻿@model IEnumerable<Api.Model.Method>
@{
    Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}
@section seo{
    <title>方法列表</title>
}
@section script{
    <script>
        $(function () {
            window.form = $('#form1');

            window.deleteUrl = '@Url.Action("Delete")';
            window.modifyCategoryUrl = '@Url.Action("ModifyCategory")';
            changeCategory();
            changeSelect();
            doSearch(1);

        });
        function doSearch(page) {
            var data = { page: page, categoryId: $('#CategoryId').val(),projectId:@Th.ProjectId};
            $.get('@Url.Action("AjaxList")', data, function (json) {
                console.log(json);
                $("#tbody").html($("#tbcontentTemplate").render(json));
                changeCategory();
            });
        }

        //选择类别下拉框
        function changeSelect(){
            $('#CategoryId').on('change',function(){
                doSearch();
            })
        }
        function changeCategory() {
            $('#tbody select').each(function () {
                $(this).on('change', function () {
                    var tr= $(this).parents('tr');
                    var methodId = $(this).find('option:selected').attr('methodId');
                    var categoryId = $(this).find('option:selected').val();
                    if (!isNaN(parseInt(methodId))) {
                        var data = {
                            methodId: methodId,
                            categoryId: categoryId,
                        }
                        $.post(modifyCategoryUrl, data, function (ajaxMsg) {
                            if(categoryId==0){
                                tr.find('.preview a').empty();
                            }else{
                                tr.find('.preview').html('<a target="_blank" href="@Url.Action("Detail", "Method",new { Th.ProjectId })>預覽</a>');
                            }
                            msgBox.showMsg(ajaxMsg);
                                })
                    }
                });
            });
        }

        //删除记录
        function deleteIds() {
            var chkIds = $("input:checked[type=checkbox]", $('#tbody'));
            if (chkIds.length == 0) {
                msgBox.showMsgErr("請選擇一項");
                return false;
            }
            var obj = window.confirm("確定刪除嗎");
            var data = $(form).serialize();
            if (obj) {
                $.post(deleteUrl, data, function (ajaxMsg) {
                    msgBox.showMsg(ajaxMsg);
                    $.each(chkIds, function () {
                        $(this).parents('tr').remove();
                    })
                });
            }
        }

        //修改tbody中tr的样式
        function changeTrClass() {
            $('#tbody tr').each(function (i, element) {
                if (i % 2 == 0) {
                    $(element).addClass('zxbejing');
                }
            });
        };
    </script>
}
<form id="form1">
    @Html.AntiForgeryToken()
    <!--左邊菜單--開始-->
    @Html.Partial("_leftMenu")
    <!--左邊菜單--結束-->
    <div class="shanpin_right">
        <!--搜索--B-->
        <div class="sptanst">
            <h2><span class="product_manage">方法列表</span></h2>
            <div class="table-responsive">
                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="table-bordered">
                    <tbody>
                        <tr>
                            <td>
                                <div class="zhengjh">
                                    <table width="100%">
                                        <tbody>
                                            <tr>
                                                @Html.DropDownList("CategoryId", BindDrop.DropCategory(), "选择类别", new { @class = "form-control" })
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </td>
                            <td valign="top">
                                <div class="newsspindss" style="float: left">
                                    <a id="lbSearch" onclick="doSearch()">
                                        <span class="glyphicon glyphicon-search forn30"></span>
                                    </a>
                                </div>
                                <div class="newsspind" style="float: right;">
                                    <a href="@Url.Action("Create", new {Th.ProjectId})" title="添加">
                                        <span class="glyphicon glyphicon-plus-sign forn30"></span>
                                    </a>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <!--搜索--E-->
        <!--内容--B-->
        <div class="tmbdliebiao" style="width: 100%">
            <div class="table-responsive">
                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="table table-bordered uytak">
                    <thead>
                        <tr style="border-bottom: 2px solid #D1D1D1;">
                            <td width="2%">
                                <input type="checkbox" onclick="selectBox(this)" />
                                <a id="btnDel" onclick="return deleteIds()">
                                    <span class="glyphicon glyphicon-trash"></span>
                                </a>
                            </td>
                            <td width="2%">
                                <b class="">
                                    <span>預覽</span>
                                    <span name="" class=""></span>
                                </b>
                            </td>
                            <td width="15%">
                                <b class="">
                                    <span>名稱</span>
                                    <span name="" class=""></span>
                                </b>
                            </td>
                            <td width="9%">
                                <b class="">
                                    <span>選擇分類</span>
                                    <span name="" class=""></span>
                                </b>
                            </td>
                            <td width="9%">
                                <b class="">
                                    <span>操作</span>
                                    <span name="" class=""></span>
                                </b>
                            </td>
                        </tr>
                    </thead>
                    <tbody id="tbody"></tbody>
                </table>
                <!--分頁--B-->
                <div class="container-fluid">
                    <div class="row-fluid">
                        <div class="span12">
                            <div id="pagination-demo" class="pagination"></div>
                        </div>
                    </div>
                </div>
                <!--分頁--E-->
            </div>
        </div>
        <!--内容--E-->
    </div>
</form>
<script type="text/x-jsrender" id="tbcontentTemplate">
    <tr>
        <td>
            <input type="checkbox" value="{{:Id}}" name="Ids">
            <a target="_blank" href="@Url.Action("Edit", new {Th.ProjectId})&Id={{:Id}}">
                <span class="glyphicon glyphicon-pencil"></span>
            </a>
        </td>
        <td class="preview">
            {{if CategoryId>0}}
            <a target="_blank" href="@Url.Action("Detail","Method",new{Area="",Th.ProjectId})&methodId={{:Id}}&categoryId={{:CategoryId}}">預覽</a>
            {{/if}}
        </td>
        <td>
            <!--表示get方法-->
            {{if HttpMethod==0}}
            <span style="color:blue">{{:Name}}</span>
            {{else}}
            <span>{{:Name}}</span>
            {{/if}}
        </td>
        <td>
            <select>
                <option methodid="{{:Id}}" value="0">請選擇</option>
                {{for Categorys}}
                <option methodid="{{:MethodId}}" value="{{:Id}}" {{if Selected}} selected="selected" {{/if}}>{{:Name}}</option>
                {{/for}}
            </select>
        </td>
        <td>
            <a target="_blank" href="@Url.Action("Create", "MethodParameter" , new {projectId=Th.ProjectId})&methodId={{:Id}}">
                添加方法參數
            </a>
        </td>
    </tr>
</script>
